var roomEditApp = roomEditApp || {};
roomEditApp.model = roomEditApp.model || {};
roomEditApp.view = roomEditApp.view || {};

$(function () {

    //model
    roomEditApp.model.roonModel = Backbone.Model.extend({
        url: window.ctx + "/rest/room/get",
        defaults: {
            room: {
                id: '',
                buildName: '',
                roomNum: '',
                isIn: 0,
                area: '',
                ownerName: '',
                createTime: '',
            }
        }
    });
    roomEditApp.view.executeView = Backbone.View.extend({
        el: "#formDiv",

        model: new roomEditApp.model.roonModel(),

        template: _.template($("#roomTmp").html()),

        //绑定元素
        events: {
            "click #saveBtn": "save",
            "click #backBtn": "back"
        },

        initialize: function () {
            var context = this;
            this.model.fetch({
                data: {
                    id: $("form").find("[name=id]").val(),
                    buildId: $("form").find("[name=buildId]").val()
                },
                success: function () {
                    context.render()
                },
                error: function () {
                    hintDialog("获取信息失败！");
                }
            });
        },
        render: function () {
            $(this.el).find("form").append(this.template(this.model.toJSON()));
            $(":text:eq(0)").focus();
        },
        save: function () {
            var context = this;
            var validate = $(context.el).find("form").validate({
                debuge: false,
                rules: {
                    buildName: {
                        required: true,
                        remote: {
                            url:window.ctx +"/rest/build/checkBuildIn",
                            type:"post",
                            dataType:"json",
                            data:{
                                buildNum:function(){
                                    return $("[name=buildNum]").val();
                                },
                            },
                        }
                    },
                    roomNum: {
                        required: true,
                        number: true,
                        remote: {
                            url:window.ctx +"/rest/room/checkRoomAdd",
                            type:"post",
                            dataType:"json",
                            data:{
                                buildName:function() {
                                    return $("[name=buildName]").val();
                                },
                                roomNum:function(){
                                    return $("[name=roomNum]").val();
                                },
                            },
                        }
                    },
                    area: {
                        required: true,
                        number:true,
                    },
                },
                messages: {
                    buildName: {
                        required: "楼宇名不能为空",
                        remote:"楼宇号不存在",
                    },
                    roomNum: {
                        required: "房号不能为空",
                        remote:"楼宇号已存在"
                    },
                    area: {
                        required: "房间面积不能为空",
                    },
                },

                submitHandler: function () {
                    var param = {id: $("[name=id]").val()};
                    param["buildName"] = $("[name=buildName]").val();
                    param["roomNum"] = $("[name=roomNum]").val();
                    param["area"] = $("[name=area]").val();
                    $.ajax({
                        url: window.ctx + "/rest/room/save",
                        type: "post",
                        dataType: "json",
                        contentType: "application/json",
                        data: JSON.stringify(param),
                        success: function (data) {
                            if (data) {
                                $.dialog.alert("保存房间信息成功！", function () {
                                    location.href = window.ctx + "/room/list";
                                });
                            } else {
                                console.dir(data);
                                hintDialog("保存房间信息失败！");
                            }
                        },
                        error: function (data) {
                            hintDialog("服务器出错！");
                        }
                    });
                    return;
                }
            });
            $(context.el).find("form").submit();
        },
        back: function () {
            location.href = window.ctx + "/room/list";
        }
    });
    new roomEditApp.view.executeView();
});